<?php

namespace app\service\Admin;

use app\model\CustomerCase;
use Exception;
use app\util\GlobalMsg;

class CustomerCaseService
{

    public static function getList(array $where = [], int $page = 0, int $pageSize = 0)
    {
        $customerCase = CustomerCase::with(['industry','scene']);

        if (!empty($where['admin_id'])) {
            $customerCase = $customerCase->where('admin_id', $where['admin_id']);
        }
        if (!empty($where['platform'])) {
            $customerCase = $customerCase->where('platform', $where['platform']);
        }
        if (!empty($where['lang'])) {
            $customerCase = $customerCase->where('lang', $where['lang']);
        }
        if (!empty($where['is_show'])) {
            $customerCase = $customerCase->where('is_show', $where['is_show']);
        }
        if (!empty($where['is_top'])) {
            $customerCase = $customerCase->where('is_top', $where['is_top']);
        }
        if (!empty($where['industry_id'])) {
            $customerCase = $customerCase->where('industry_id', $where['industry_id']);
        }
        if (!empty($where['scene_id'])) {
            $customerCase = $customerCase->where('scene_id', $where['scene_id']);
        }
        if (!empty($where['short_title'])) {
            $customerCase = $customerCase->where('short_title','like', '%'.$where['short_title'].'%');
        }
        if (!empty($where['title'])) {
            $customerCase = $customerCase->where('title','like', '%'. $where['title'].'%');
        }

        $count = $customerCase->count();

        if ($page > 0 && $pageSize > 0) {
            $customerCase = $customerCase->forPage($page, $pageSize);
        }

        $list = $customerCase->orderBy('is_top', 'asc')->orderBy('id', 'desc')->get()->toArray();

        return ['count' => $count, 'list' => $list];
    }

    public static function getAll(array $where = [])
    {
        $customerCase = new CustomerCase();

        if (!empty($where['admin_id'])) {
            $customerCase = $customerCase->where('admin_id', $where['admin_id']);
        }
        if (!empty($where['platform'])) {
            $customerCase = $customerCase->where('platform', $where['platform']);
        }
        if (!empty($where['lang'])) {
            $customerCase = $customerCase->where('lang', $where['lang']);
        }
        if (!empty($where['is_show'])) {
            $customerCase = $customerCase->where('is_show', $where['is_show']);
        }
        if (!empty($where['is_top'])) {
            $customerCase = $customerCase->where('is_top', $where['is_top']);
        }
        if (!empty($where['industry_id'])) {
            $customerCase = $customerCase->where('industry_id', $where['industry_id']);
        }
        if (!empty($where['scene_id'])) {
            $customerCase = $customerCase->where('scene_id', $where['scene_id']);
        }
        if (!empty($where['short_title'])) {
            $customerCase = $customerCase->where('short_title','like', '%'.$where['short_title'].'%');
        }
        if (!empty($where['title'])) {
            $customerCase = $customerCase->where('title','like', '%'. $where['title'].'%');
        }

        return $customerCase->orderBy('is_top', 'asc')->orderBy('id', 'desc')->get()->toArray();
    }

    public static function getOne(int $id = 0)
    {
        $customerCase = CustomerCase::where('id', $id)->first();
        if ($customerCase == null) {
            throw new Exception(GlobalMsg::GET_HAS_NO);
        }
        return $customerCase;
    }

    public static function add(array $where = [])
    {

        $customerCase = new CustomerCase();
        if (!empty($where['id'])) {
            throw new Exception(GlobalMsg::ADD_ID);
        }
        isset($where['admin_id']) && $customerCase->admin_id = $where['admin_id'];
        isset($where['content']) && $customerCase->content = $where['content'];
        isset($where['is_show']) && $customerCase->is_show = $where['is_show'];
        isset($where['is_top']) && $customerCase->is_top = $where['is_top'];
        isset($where['lang']) && $customerCase->lang = $where['lang'];
        isset($where['pic']) && $customerCase->pic = $where['pic'];
        isset($where['pics']) && $customerCase->pics = $where['pics'];
        isset($where['video_url']) && $customerCase->video_url = $where['video_url'];
        isset($where['platform']) && $customerCase->platform = $where['platform'];
        isset($where['industry_id']) && $customerCase->industry_id = $where['industry_id'];
        isset($where['scene_id']) && $customerCase->scene_id = $where['scene_id'];
        isset($where['short_title']) && $customerCase->short_title = $where['short_title'];
        isset($where['sort']) && $customerCase->sort = $where['sort'];
        isset($where['title']) && $customerCase->title = $where['title'];
        isset($where['url']) && $customerCase->url = $where['url'];
        isset($where['view_count']) && $customerCase->view_count = $where['view_count'];


        $res = $customerCase->save();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $res;
    }

    public static function save(array $where = [])
    {
        if (empty($where['id'])) {
            throw new Exception(GlobalMsg::SAVE_NO_ID);
        }
        $customerCase = CustomerCase::where('id', $where['id'])->first();
        if ($customerCase == null) {
            throw new Exception(GlobalMsg::SAVE_HAS_NO);
        }

        isset($where['admin_id']) && $customerCase->admin_id = $where['admin_id'];
        isset($where['content']) && $customerCase->content = $where['content'];
        isset($where['is_show']) && $customerCase->is_show = $where['is_show'];
        isset($where['is_top']) && $customerCase->is_top = $where['is_top'];
        isset($where['lang']) && $customerCase->lang = $where['lang'];
        isset($where['pic']) && $customerCase->pic = $where['pic'];
        isset($where['pics']) && $customerCase->pics = $where['pics'];
        isset($where['video_url']) && $customerCase->video_url = $where['video_url'];
        isset($where['platform']) && $customerCase->platform = $where['platform'];
        isset($where['industry_id']) && $customerCase->industry_id = $where['industry_id'];
        isset($where['scene_id']) && $customerCase->scene_id = $where['scene_id'];
        isset($where['short_title']) && $customerCase->short_title = $where['short_title'];
        isset($where['sort']) && $customerCase->sort = $where['sort'];
        isset($where['title']) && $customerCase->title = $where['title'];
        isset($where['url']) && $customerCase->url = $where['url'];
        isset($where['view_count']) && $customerCase->view_count = $where['view_count'];


        $res = $customerCase->save();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $res;
    }

    public static function delete(int $id = 0)
    {
        $customerCase = CustomerCase::where('id', $id)->first();
        if ($customerCase == null) {
            throw new Exception(GlobalMsg::DEL_HAS_NO);
        }
        $res = $customerCase->delete();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $res;
    }
}
